package com.dao.impl;

import com.dao.BaseDeo;
import com.dao.UserDao;
import com.entity.short_reviews.UserShortReviews;
import com.entity.user.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wang_
 * @version 2024.3.4.1
 * @description
 * @date 2025/7/11
 */
public class UserDaoImpl extends BaseDeo implements UserDao {
//	@Override
//	public UserDetailed getUser(int id, String password) {
//		linkConn();
//		//language=MySQL
//		String sql = """
//		             	select * from user u
//		             	INNER JOIN user_group g on g.id = u.id
//		             	where u.id = ? and password = ?
//		             """;
//		ResultSet rs = exeQuery(sql, id, password);
//		List<UserDetailed> userDetailedList = getUserDetaileds(rs);
//		closeAll(rs);
//		return userDetailedList.getFirst();
//	}
	
	/// 获取用户评论
	private static List<UserShortReviews> getUserShortReviewsList(ResultSet rs) throws SQLException {
		List<UserShortReviews> userShortReviewsList = new ArrayList<>();
		while (rs.next()) {
			UserShortReviews userShortReviews = new UserShortReviews();
			userShortReviews.setId(rs.getInt("sr.id"));
			userShortReviews.setFatherId(rs.getInt("sr.father_id"));
			userShortReviews.setPublisherId(rs.getInt("sr.publisher_id"));
			userShortReviews.setContent(rs.getString("sr.content"));
			userShortReviews.setLikeCount(rs.getInt("sr.like_count"));
			userShortReviews.setDate(rs.getString("sr.date"));
			userShortReviewsList.add(userShortReviews);
		}
		return userShortReviewsList;
	}
	
	@Override
	public int updateLastOnlineTime(int id) {
		linkConn();
		//language=MySQL
		String sql = "update user set last_online_time = ? where id = ?";
		LocalDateTime now = LocalDateTime.now();
		int i = exeUpdate(sql, now.toLocalDate() + " " + now.toLocalTime(), id);
		closeAll();
		return i;
	}

//
//	/// 获取用户信息
//	private List<UserDetailed> getUserDetaileds(ResultSet rs) {
//		List<UserDetailed> userDetailedList = new ArrayList<>();
//		try {
//			//language=MySQL
//			String sql = """
//			             	select * from user_short_reviews sr
//			                 INNER JOIN user u on u.id = sr.user_id
//			             	where u.id = ?
//			             """;
//			while (rs.next()) {
//				UserDetailed userDetailed = new UserDetailed();
//
//				// 用户基础信息
//				User user = new User();
//				user.setId(rs.getInt("u.id"));
//				user.setAvatar(rs.getString("u.avatar"));
//				user.setName(rs.getString("u.name"));
//				user.setLastOnlineTime(rs.getString("u.last_online_time"));
//				user.setRegistrationDate(rs.getString("u.registration_date"));
//				user.setUserGroupId(rs.getInt("u.user_group_id"));
//				user.setPassword(rs.getString("u.password"));
//				userDetailed.setUser(user);
//
//				// 用户组信息
//				UserGroup userGroup = new UserGroup();
//				userGroup.setId(rs.getInt("g.id"));
//				userGroup.setName(rs.getString("g.name"));
//				userDetailed.setGroup(userGroup);
//
//				// 获取用户评论
//				ResultSet rs1 = exeQuery(sql, user.getId());
//				List<UserShortReviews> userShortReviewsList = new ArrayList<>();
//				try {
//					userShortReviewsList = getUserShortReviewsList(rs1);
//				} catch (SQLException e) {
//					LOG.error("获取用户评论异常");
//					LOG.error(e);
//				}
//				if (rs1 != null) {
//					rs1.close();
//				}
//				userDetailed.setShortReviews(userShortReviewsList);
//
//				userDetailedList.add(userDetailed);
//			}
//		} catch (SQLException e) {
//			LOG.error("获取用户信息异常");
//			LOG.error(e);
//		}
//		return userDetailedList;
//	}
	
	@Override
	public List<User> selectAll() {
		linkConn();
		//language=MySQL
		String sql = "select * from user";
		ResultSet rs = exeQuery(sql);
		List<User> List = new ArrayList<>();
		try {
			while (rs.next()){
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setAvatar(rs.getString("avatar"));
				user.setName(rs.getString("name"));
				String lastOnlineTime = rs.getString(("last_online_time"));
				if(lastOnlineTime !=null){
					user.setLastOnlineTime(lastOnlineTime);
				}
				user.setRegistrationDate(rs.getString("registration_date"));
				user.setUserGroupId(rs.getInt("user_group_id"));
				user.setPassword(rs.getString("password"));
				List.add(user);
			}
		} catch (SQLException e) {
			LOG.debug("获取用户信息异常");
			LOG.debug(e);
		}
		closeAll(rs);
		System.out.println("获取用户信息成功"+List);
		return List;
	}
	
}
